package com.rongc.client.core.utils;

import android.app.Activity;
import android.content.Context;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

/**
 * Created by Bruce on 2016/11/25.
 */
public class SystemUtil {
    /**
     * 将dip转换为px
     *
     * @param dipValue
     * @return
     */
    public static int dip2px(Context context , float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    /**
     * 将px转换为dip
     *
     * @param pxValue
     * @return
     */
    public static int px2dip(Context context , float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static void hideSystemKeyboad(Activity activity) {

        InputMethodManager manager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (!manager.isActive()) {
            return;
        }
        // 判断隐藏软键盘是否弹出
        boolean flag = activity.getWindow().getAttributes().softInputMode == WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE;

        if (!flag) {

            // 隐藏软键盘
            manager.hideSoftInputFromWindow(activity.getCurrentFocus()
                            .getApplicationWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);

        }
        // InputMethodManager imm = (InputMethodManager) context
        // .getSystemService(Context.INPUT_METHOD_SERVICE);
        // imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

    }
}
